package ar.com.spsolutions.splilabrary.view.pages.components;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;

import ar.com.spsolutions.splibrary.general.IFindByNameService;

public class FindByNameComponent<T> extends Panel implements IHeaderContributor {

	private static final long serialVersionUID = -9204009789335252076L;

	private String name;
	private TextField<String> textName;
	private List<T> selections;

	private IFindByNameService<T> service;

	private ListMultipleChoice<T> listSelector;

	private String selectorId;

	public FindByNameComponent(final String id,
			final IFindByNameService<T> service) {
		super(id);
		this.selectorId = id;
		this.service = service;
		this.addComponents();
	}

	private void addComponents() {
		final Form<FindByNameComponent<T>> form = new Form<FindByNameComponent<T>>(
				"form", new CompoundPropertyModel<FindByNameComponent<T>>(this));
		this.addTextName(form);
		this.addListSelector(form);
		this.add(form);
	}

	private void addListSelector(final Form<FindByNameComponent<T>> form) {
		this.setListSelector(new ListMultipleChoice<T>("selector",
				new PropertyModel<List<T>>(this, "selections"), this
						.getResults()));
		this.getListSelector().setOutputMarkupId(true);
		form.add(this.getListSelector());
	}

	private List<T> getResults() {
		return this.getService().findByLikeName(this.getName());
	}

	private void addTextName(final Form<FindByNameComponent<T>> form) {
		this.setTextName(new TextField<String>("name"));
		this.getTextName().add(
				new AjaxFormComponentUpdatingBehavior("onchange") {

					private static final long serialVersionUID = 2185041815177171700L;

					@Override
					protected void onUpdate(final AjaxRequestTarget target) {
						target.add(FindByNameComponent.this.getListSelector());
					}

				});
		form.add(this.getTextName());
	}

	@Override
	public void renderHead(final IHeaderResponse response) {
		super.renderHead(response);
		response.renderOnDomReadyJavaScript("$('.mselector').dropdownchecklist();");
	}

	// --accessors--

	public String getName() {
		return this.name == null ? "" : this.name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public TextField<String> getTextName() {
		return this.textName;
	}

	public void setTextName(final TextField<String> textName) {
		this.textName = textName;
	}

	public IFindByNameService<T> getService() {
		return this.service;
	}

	public void setService(final IFindByNameService<T> service) {
		this.service = service;
	}

	public List<T> getSelections() {
		return this.selections == null ? new ArrayList<T>() : this.selections;
	}

	public void setSelections(final List<T> selections) {
		this.selections = selections;
	}

	public ListMultipleChoice<T> getListSelector() {
		return this.listSelector;
	}

	public void setListSelector(final ListMultipleChoice<T> listSelector) {
		this.listSelector = listSelector;
	}

	public String getSelectorId() {
		return this.selectorId;
	}

	public void setSelectorId(final String selectorId) {
		this.selectorId = selectorId;
	}

}
